package com.egdigital.allzic.ui.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Point;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Display;
import android.view.WindowManager;
import android.widget.ImageView;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.egdigital.allzic.R;
import com.egdigital.allzic.ui.PagerTransformer;
import com.egdigital.allzic.ui.fragment.FragmentTutoPage;
import com.egdigital.allzic.utils.ImageManager;
import com.egdigital.allzic.utils.Utils;
import com.viewpagerindicator.CirclePageIndicator;

public class ActivityTuto extends SherlockFragmentActivity {

	private static final String	TAG					= ActivityLauncher.class.getSimpleName();	// log
	public static int			mAssetFolderSize	= 0;
	public static final String	mAssetFolder		= "gallery";
	public static final String	mImageRootName		= "intro_slide";
	private ImageManager		mImageManager;
	public static int			mScreenHeight		= 0;
	public static int			mScreenWidth		= 0;
	private ViewPager			mPager;
	private ImageView			mImage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		this.setVolumeControlStream(AudioManager.STREAM_MUSIC); // set
		setContentView(R.layout.activity_tuto);
		mPager = (ViewPager) findViewById(R.id.tuto_pager);
		mImage = (ImageView) findViewById(R.id.activity_tuto_background);
		//
		mImageManager = new ImageManager(getApplicationContext());
		mImageManager.setOptionsTuto();
		getScreenSize();
		mImageManager.setImageLoader(mScreenWidth, mScreenHeight);
		Utils.setImageFromUrl(mImageManager, "assets://background/intro_fond.jpg", mImage);
		mPager.setOffscreenPageLimit(1);
		List<FragmentTutoPage> fragments = new ArrayList<FragmentTutoPage>();
		String[] gallery;
		try {
			gallery = getAssets().list(mAssetFolder);
			mAssetFolderSize = gallery.length;
			for (int i = 0; i < gallery.length; i++) {
				Bundle bundle = new Bundle();
				bundle.putParcelable("song", null);
				bundle.putInt("name", i + 1);
				if (i == gallery.length - 1) {
					bundle.putBoolean("is_last", true);
				} else {
					bundle.putBoolean("is_last", false);
				}
				fragments.add((FragmentTutoPage) FragmentTutoPage.instantiate(this, FragmentTutoPage.class.getName(), bundle));
			}
		} catch (IOException e) {
		}
		PagerAdapter mAdapterPager = new PagerAdapter(super.getSupportFragmentManager(), fragments);
		mPager.setAdapter(mAdapterPager);
		mPager.setPageTransformer(true, new PagerTransformer());
		CirclePageIndicator titleIndicator = (CirclePageIndicator) findViewById(R.id.indicator); // Bind the title indicator to the adapter
		titleIndicator.setViewPager(mPager);
		super.onCreate(savedInstanceState);
	}

	public static class PagerAdapter extends FragmentPagerAdapter {

		private final List	fragments;

		// fournit a l'adapter la liste des fragments a afficher
		public PagerAdapter(FragmentManager fm, List fragments) {
			super(fm);
			this.fragments = fragments;
		}

		@Override
		public SherlockFragment getItem(int position) {
			return (SherlockFragment) this.fragments.get(position);
		}

		@Override
		public int getCount() {
			return this.fragments.size();
		}
	}

	public void moveToHome(boolean doAnimation) {
		Intent intent = new Intent(this, ActivityHome.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
		finish();
		if (doAnimation) {
			overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
		}
	}

	@SuppressLint("NewApi")
	private void getScreenSize() {
		// ex : 900*1440
		// ex : 1080*1728
		final int maxHeight = 1440;
		final int maxWidth = 900;
		Point size = new Point();
		WindowManager w = getWindowManager();
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
			w.getDefaultDisplay().getSize(size);
			mScreenWidth = size.x;
			mScreenHeight = size.y;
		} else {
			Display d = w.getDefaultDisplay();
			mScreenWidth = d.getWidth();
			mScreenHeight = d.getHeight();
		}
		if (mScreenWidth > maxWidth) {
			mScreenWidth = maxWidth;
		}
		if (mScreenHeight > maxHeight) {
			mScreenHeight = maxHeight;
		}
	}
}
